<template>
	<rx-dialog @ok="handOk" @cancel="cancel">
        <RxVueMonaco
            ref="staticData"
            :value="staticData"
            language="json"
            class="monacoEditor"
            style="height: 100%"
            @change="valueChange"
        >
            <template #toolbar>
                <div>
                    <a-button size="small" style="margin-left: 8px" @click="restoreData">{{i18n('restoreData','还原数据')}}</a-button>
                </div>
            </template>
        </RxVueMonaco>
	</rx-dialog>
</template>

<script>
/**
 * 静态数据对话框
 */
import {RxDialog,Util,RxVueMonaco} from "@lowcode/jpaas-base-lib";
import {defData} from "../js/static-data.js";

export default {
	name: "StaticDataDialog",
	components: {
		RxDialog,
        RxVueMonaco
	},
	props: {
		config:{
			type:Object
		},
		curComponent:{
			type:Object
		},
		destroy: {
			type: Function
		},
	},
	data() {
		return {
			staticData:"",
		}
	},
	created() {
		this.initData();
	},
	methods: {
		i18n(name,text,key){
			return this.$ti18(name,text,"staticDataLang",key);
		},
		initData(){
			if( this.config.dataSources &&  this.config.dataSources.staticData){
				this.staticData=this.config.dataSources.staticData
			}else {
				this.setDefData(this.curComponent.type);
			}
		},
		restoreData(){
			let self=this;
			this.$confirm({
				content: this.i18n('restoreConfirm',"确定要还原当前数据吗？"),
				zIndex:99999,
				onOk() {
					self.initData();
                    self.$refs.staticData.setValue(self.staticData);
				},
				onCancel() {
				},
			});
		},
		setDefData(type){
			if(!type){
				return;
			}
			let data ="";
			if(type=="chart"){
				data = defData[this.config.chartType];
			}else {
				data = defData[type];
			}
			this.staticData=data ? JSON.stringify(data, null, "\t") : "";
		},
		cancel() {
			Util.closeWindow(this, "cancel");
		},
		handOk() {
			Util.closeWindow(this, "ok",this.staticData);
		},
        valueChange(value){
            this.staticData=value;
        }
	},
	watch: {},
}
</script>

<style scoped>

</style>
